package com.wanxi.tool;

import java.util.Random;

/**
 * 工具类
 */
public class Tool {
    /**
     * 根据数组的长度定义数组
     *
     * @param length
     * @return
     */
    public static int[] getIntArr(int length) {
        int[] arr = new int[length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Random().nextInt(99);
        }
        return arr;
    }

    public static int[] getIntArr(int length, int max) {
        int[] arr = new int[length];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(max);
        }
        return arr;
    }

    public static int[] getIntArr(int length, int min, int max) {
        int[] arr = new int[length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Random().nextInt(max - min) + min;
        }
        return arr;
    }

    /**
     * 求数组的和
     *
     * @param intArr
     * @return
     */
    public static int sumArr(int[] intArr) {
        int sum = 0;
        for (int i = 0; i < intArr.length; i++) {
            sum += intArr[i];
        }
        return sum;
    }

    /**
     * 求数组的和
     *
     * @param intArr
     * @return
     */
    public static int maxArr(int[] intArr) {
        int max = intArr[0];
        for (int i = 1; i < intArr.length; i++) {
            if (max < intArr[i]) {
                max = intArr[i];
            }
        }
        return max;
    }

    /**
     * 求数组最小值
     *
     * @param intArr
     * @return
     */
    public static int minArr(int[] intArr) {
        int min = intArr[0];
        for (int i = 1; i < intArr.length; i++) {
            if (min > intArr[i]) {
                min = intArr[i];
            }
        }
        return min;
    }
    public static int getRandom(int num){
        return new Random().nextInt(num);
    }
}
